//
//  YZMRecommandView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/28.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMRecommandView.h"
#import "YZMAppStyle.h"
#import <Masonry/Masonry.h>

static const float alert_view_margin_left = 20.0;

@interface YZMRecommandView()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *footLabel;

@end

@implementation YZMRecommandView{
    
    UIView *_containerView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame: frame];
    if (self) {
        
        [self createObjects];
        [self addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(dismissGesture:)]];
    }
    return self;
}

- (void)createObjects
{
    self.backgroundColor = [UIColor colorWithRed:14/255 green:24/255 blue:33/255 alpha:.7];
    _containerView = [UIView new];
    _containerView.layer.cornerRadius = 5.0;
    _containerView.layer.masksToBounds = YES;
    _containerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_containerView];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = [YZMAppStyle colorForBlackText];
    self.titleLabel.font = [YZMAppStyle largeSizeFont];
    [_containerView addSubview: self.titleLabel];
    
    UIView *lineView = [YZMAppStyle splitLine];
    lineView.backgroundColor = [YZMAppStyle colorForGrayUnable];
    [_containerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLabel.mas_bottom).offset(7);
        make.left.right.equalTo(_containerView);
    }];
    
    self.contentLabel = [UILabel new];
    self.contentLabel.textColor = [YZMAppStyle colorForBlackText];
    self.contentLabel.font = [YZMAppStyle mediumSizeFont];
    self.contentLabel.numberOfLines = 0;
    [_containerView addSubview: self.contentLabel];
    
    self.footLabel = [UILabel new];
    self.footLabel.textColor = [YZMAppStyle colorForLightGrayText];
    self.footLabel.font = [YZMAppStyle smallSizeFont];
    [_containerView addSubview: self.footLabel];
    self.footLabel.numberOfLines = 0;
}

- (void)assignConstaints
{
    UIView *lastView = nil;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_containerView).with.offset(alert_view_margin_left);
        make.left.equalTo(_containerView).offset(alert_view_margin_left);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(15);
        make.left.equalTo(_containerView).offset(alert_view_margin_left);
        make.right.equalTo(_containerView).with.offset(-alert_view_margin_left);
    }];
    
    lastView = self.contentLabel;
    
    if (self.footLabel.text && self.footLabel.text.length != 0) {
        
        [self.footLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).with.offset(15);
            make.leading.equalTo(self.titleLabel);
            make.trailing.equalTo(_containerView).with.offset(-alert_view_margin_left);
            make.bottom.equalTo(_containerView).with.offset(-alert_view_margin_left);
        }];
        
        lastView = self.footLabel;
        
    }else{
        
        self.footLabel.hidden = YES;
    }
    
    if (self.customView) {
        
        CGSize size = self.customView.frame.size;
        
        [self.customView mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(lastView).offset(15);
            maker.height.mas_equalTo(size.height);
            maker.width.mas_equalTo(size.width);
            maker.centerX.equalTo(_containerView);
        }];
        
        lastView = self.customView;
    }
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.width.equalTo(self).offset(-40);
        maker.centerX.centerY.equalTo(self);
        maker.bottom.equalTo(lastView).offset(alert_view_margin_left);
    }];
}


- (void)setCustomView:(UIView *)customView{
    
    if (_customView) {
        
        [_customView removeFromSuperview];
    }
    _customView = customView;
    [self addSubview:_customView];
}


- (void)showInView: (UIView *)view
{
    self.alpha = 0;
    [view addSubview: self];
    self.frame = view.bounds;
    [self assignConstaints];
    [UIView animateWithDuration: .4 animations:^{
        self.alpha = 1;
    }];
}

- (void)dismissSelf
{
    self.alpha = 1;
    [UIView animateWithDuration: .4f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dismissGesture: (UIGestureRecognizer *)gesture
{
    [self dismissSelf];
}

@end
